﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using DLL;

namespace CuaHangBangDia
{
    public partial class Form_TaiKhoan : DevComponents.DotNetBar.RibbonForm
    {
        TAI_KHOAN tk;
        public Form_TaiKhoan()
        {
            InitializeComponent();
        }

        TaiKhoanManager manager = new TaiKhoanManager();

        private void Form_TaiKhoan_Load(object sender, EventArgs e)
        {
            load();
        }

        private void load()
        {
            grvTaiKhoan.DataSource = getAll(manager.GetAll());
        }
        DataTable getAll(List<TAI_KHOAN> L)
        {
            DataTable t = new DataTable();
            t.Columns.Add("ID");
            t.Columns.Add("Tài khoản");
            t.Columns.Add("Mật khẩu");
            t.Columns.Add("Quyền quản lý");
            foreach (var item in L)
            {
                t.Rows.Add(item.ID, item.TaiKhoan, item.MatKhau, item.Quyen);
            }
            return t;
        }
        private void btnThemTaiKhoan_Click(object sender, EventArgs e)
        {
            Form_TaiKhoan_Them_Sua frm = new Form_TaiKhoan_Them_Sua();
            frm.ShowDialog();
            load();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSuaTaiKhoan_Click(object sender, EventArgs e)
        {
            if (tk != null)
            {
                Form_TaiKhoan_Them_Sua frm = new Form_TaiKhoan_Them_Sua(tk);
                frm.ShowDialog();
                load();
            }
            else
                MessageBox.Show("Vui lòng chọn tài khoản cần sửa");
        }

        private void grvTaiKhoan_Click(object sender, EventArgs e)
        {
            tk = new TAI_KHOAN();
            try
            {
                DataGridViewRow grwRow = grvTaiKhoan.SelectedRows[0];
                DataRowView rw = grwRow.DataBoundItem as DataRowView;
                tk.ID = txtID.Text = rw["ID"].ToString();
                tk.TaiKhoan = rw[1].ToString();
                tk.MatKhau = rw[2].ToString();
                if (rw[3].ToString() == "Yes")
                    tk.Quyen = "Yes";
                else
                {
                    tk.Quyen = "No";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Có lỗi xảy ra");
            }
        }

        private void btnXoaTaiKhoan_Click(object sender, EventArgs e)
        {
            try
            {
                string ID = txtID.Text;
                manager.XoaTK(ID);
                MessageBox.Show("Xoá thành công");
                load();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Có lỗi xảy ra");
            }
        }

        private void btnTimMatKhau_Click(object sender, EventArgs e)
        {
            Form_TaiKhoan_TimKiem frm = new Form_TaiKhoan_TimKiem();
            frm.ShowDialog();
            if (frm.TaiKhoan != null || frm.TaiKhoan.Count > 0)
                grvTaiKhoan.DataSource = getAll(frm.TaiKhoan);
        }
    }
}